using System;
using Microsoft.EntityFrameworkCore;
using CR.Domain.Entities;

namespace CR.Domain.DbContext
{
    /// <summary>
    /// 溯源系统数据库上下文
    /// </summary>
    public class TraceabilityDbContext : Microsoft.EntityFrameworkCore.DbContext
    {
        public TraceabilityDbContext(DbContextOptions<TraceabilityDbContext> options) : base(options)
        {
        }

        /// <summary>
        /// 用户表
        /// </summary>
        public DbSet<User> Users { get; set; }

        /// <summary>
        /// 溯源管理表
        /// </summary>
        public DbSet<TraceabilityManagement> TraceabilityManagements { get; set; }

        /// <summary>
        /// 产品检验报告表
        /// </summary>
        public DbSet<ProductInspectionReport> ProductInspectionReports { get; set; }

        /// <summary>
        /// 养殖信息表
        /// </summary>
        public DbSet<BreedingInfo> BreedingInfos { get; set; }

        /// <summary>
        /// 保健信息表
        /// </summary>
        public DbSet<HealthcareInfo> HealthcareInfos { get; set; }

        /// <summary>
        /// 动检证表
        /// </summary>
        public DbSet<AnimalQuarantineCertificate> AnimalQuarantineCertificates { get; set; }

        /// <summary>
        /// 养殖企业信息表
        /// </summary>
        public DbSet<BreedingEnterpriseInfo> BreedingEnterpriseInfos { get; set; }

        /// <summary>
        /// 企业照片表
        /// </summary>
        public DbSet<EnterprisePhoto> EnterprisePhotos { get; set; }

        /// <summary>
        /// 屠宰信息表
        /// </summary>
        public DbSet<SlaughterInfo> SlaughterInfos { get; set; }

        /// <summary>
        /// 相关数据表
        /// </summary>
        public DbSet<RelatedData> RelatedDatas { get; set; }

        /// <summary>
        /// 屠宰企业信息表
        /// </summary>
        public DbSet<SlaughterEnterpriseInfo> SlaughterEnterpriseInfos { get; set; }

        /// <summary>
        /// 深加工信息表
        /// </summary>
        public DbSet<DeepProcessingInfo> DeepProcessingInfos { get; set; }

        /// <summary>
        /// 食品安全信息表
        /// </summary>
        public DbSet<FoodSafetyInfo> FoodSafetyInfos { get; set; }

        /// <summary>
        /// 原料信息表
        /// </summary>
        public DbSet<RawMaterialInfo> RawMaterialInfos { get; set; }

        /// <summary>
        /// 辅料信息表
        /// </summary>
        public DbSet<AuxiliaryMaterialInfo> AuxiliaryMaterialInfos { get; set; }

        /// <summary>
        /// 添加剂信息表
        /// </summary>
        public DbSet<AdditiveInfo> AdditiveInfos { get; set; }

        /// <summary>
        /// 包材信息表
        /// </summary>
        public DbSet<PackagingMaterialInfo> PackagingMaterialInfos { get; set; }

        /// <summary>
        /// 供应商信息相关资料表
        /// </summary>
        public DbSet<SupplierInfo> SupplierInfos { get; set; }

        /// <summary>
        /// 深加工企业信息表
        /// </summary>
        public DbSet<ProcessingEnterpriseInfo> ProcessingEnterpriseInfos { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 配置实体关系和索引
            modelBuilder.Entity<TraceabilityManagement>()
                .HasIndex(t => t.TraceabilityCode)
                .IsUnique();

            // 配置用户表索引
            modelBuilder.Entity<User>()
                .HasIndex(u => u.Username)
                .IsUnique();

            modelBuilder.Entity<User>()
                .HasIndex(u => u.Email)
                .IsUnique()
                .HasFilter("[Email] IS NOT NULL");

            modelBuilder.Entity<User>()
                .HasIndex(u => u.PhoneNumber)
                .IsUnique()
                .HasFilter("[PhoneNumber] IS NOT NULL");
        }
    }
} 